package threewings;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

/**
 * manages keylistening
 *
 * @author Tyler Whitehouse.
 *         Created Oct 21, 2013.
 */
public class MyKeyListener implements KeyListener {

	private boolean rightPressed = false, leftPressed = false, upPressed = false, downPressed = false;
	private boolean spacePressed;
	private boolean[] keysDown;
	
	/**
	 * Creates an instance of this
	 *
	 */
	public MyKeyListener(){
		this.keysDown = new boolean[222];
	}

	@Override
	public void keyPressed(KeyEvent e) {
		this.keysDown[e.getKeyCode()] = true;
		/* *
		switch (e.getKeyCode()){
		case KeyEvent.VK_LEFT:
			this.leftPressed = true;
			break;
			
		case KeyEvent.VK_UP:
			this.upPressed = true;
			break;
			
		case KeyEvent.VK_RIGHT:
			this.rightPressed = true;
			break;
			
		case KeyEvent.VK_DOWN:
			this.downPressed = true;
			break;
		case KeyEvent.VK_SPACE:
			this.spacePressed=true;
			break;
			
		default:
			break;
		}
		/* */
		if(e.getKeyCode()==KeyEvent.VK_ESCAPE)
			System.exit(0);
	}

	@Override
	public void keyReleased(KeyEvent e) {
		this.keysDown[e.getKeyCode()] = false;
		/* *
		switch (e.getKeyCode()){
		case KeyEvent.VK_LEFT:
			this.leftPressed = false;
			break;
			
		case KeyEvent.VK_UP:
			this.upPressed = false;
			break;
			
		case KeyEvent.VK_RIGHT:
			this.rightPressed = false;
			break;
			
		case KeyEvent.VK_DOWN:
			this.downPressed = false;
			break;
		case KeyEvent.VK_SPACE:
			this.spacePressed=false;
			break;
			
		default:
			break;
		}
		/* */
	}

	@Override
	public void keyTyped(KeyEvent e) {
		//nothing to see here
	}

	/**
	 * 
	 * @return whether up is pressed
	 */
	public boolean up(){
		return this.upPressed;
	}
	
	/**
	 * 
	 *
	 * @return whether down is pressed
	 */
	public boolean down(){
		return this.downPressed;
	}
	
	/**
	 * 
	 *
	 * @return whether right is pressed
	 */
	public boolean right(){
		return this.rightPressed;
	}
	
	/**
	 * 
	 *
	 * @return whether left is pressed
	 */
	public boolean left(){
		return this.leftPressed;
	}
	/**
	 * 
	 *
	 * @return whether space is pressed
	 */
	public boolean space() {
		return this.spacePressed;
	}
	
	public boolean isPressed(int code){
		try {
			return this.keysDown[code];
		} catch (Exception e){
			return false;
		}
	}
}
